require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#adVideo",
    data:{
        loadFlag:false,
        data:{
            uid:"",  //id
            pageNumber:"1",//页数
            sortId:"rank", // 排序字段 默认排序 rank 最多浏览 play_count 最新上传 create_date
            sortValue:"asc" // 排序值 asc 升 desc 降
        },
        id:"",
        allAlbumByUserId:{
            list:{}
        },//相册
        allProductByUserId:{
            list:{}
        },//产品
        allProductByUserId1:[],
        allProductByUserId2:[],
        advById:{},
        advRecommendByAdvId:{}
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.loginUid=this.searchObject&&this.searchObject.loginUid;
        this.data.uid=this.searchObject&&this.searchObject.uid;
        this.id=this.searchObject&&this.searchObject.id;
        this.getAdvById();
        this.getAdvRecommendByAdvId();
        this.getAllAlbumByUserId();
        this.getAllProductByUserId();
        lazyload();
    },
    methods:{
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取播放视频信息
        getAdvById:function(){
            var that=this;
            this.ajax({url:"/getAdvById",data:{id:that.id},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.advById=b.json.data;
                    sewisePlayer(that.advById.imgSrc)
                }else{
                    vm.$refs.prompt.toast("获取播放视频信息失败！")
                }
            });
        },
        //通过视频id获取相关推荐信息
        getAdvRecommendByAdvId:function(){
            var that=this;
            this.ajax({url:"/getAdvRecommendByAdvId",data:{id:that.id},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.advRecommendByAdvId=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取视频推荐信息失败！")
                }
            });
        },
        //通过用户id 获取相册列表
        getAllAlbumByUserId:function(){
            var that=this;
            var data={
                uid:that.data.uid,
                pageNumber:1
            };
            this.ajax({url:"/getAllAlbumByUserId",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.allAlbumByUserId=b.json.data;
                    lazyload();
                }else{
                    that.allAlbumByUserId={};
                    vm.$refs.prompt.toast("获取相册失败！")
                }
            });
        },
        //通过用户id 获取产品列表
        getAllProductByUserId:function(){
            var that=this;
            var data={
                uid:that.data.uid,
                pageNumber:that.data.pageNumber,
                sortId:that.data.sortId,
                sortValue:that.data.sortValue
            };
            this.ajax({url:"/getAllProductByUserId",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.allProductByUserId=b.json.data;
                    that.allProductByUserId1=that.allProductByUserId.list;
                    if(that.allProductByUserId.pageNumber>1){
                        if(that.allProductByUserId.list.length>0) {
                            that.allProductByUserId2 = that.allProductByUserId2.concat(b.json.data.list);
                        }
                    }else{
                        that.allProductByUserId2= that.allProductByUserId1;
                    }
                    //console.log(that.hotAdvList);
                    vm.loadFlag=false;
                    lazyload();
                    console.log(that.allProductByUserId2);
                }else{
                    that.allProductByUserId={};
                    that.allProductByUserId1=[];
                    that.allProductByUserId2=[];
                    vm.$refs.prompt.toast("获取产品失败！")
                }
            });
        }
    }
});
function sewisePlayer(url){
    SewisePlayer.setup({
        server: "vod",
        type: "mp4",
        videourl:url,
        skin: "vodWhite",
        poster: url+"?vframe/jpg/offset/2/w/480/h/360",
        autostart: "true",
        lang:"zh_CN"
    }, "player");
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag = vm.allProductByUserId.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.data.pageNumber++;
            vm.getAllProductByUserId();
        }else{
            lazyload();
          vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function lazyload(){
    $("img.lazy").lazyload();
}